package com.example.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Book;
import com.example.demo.entity.News;
import com.example.demo.mapper.BookMapper;
import com.example.demo.mapper.RecommendMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Resource
    BookMapper BookMapper;


    @PostMapping
    public Result<?> save(@RequestBody Book Book){
        BookMapper.insert(Book);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Book Book){
        BookMapper.updateById(Book);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){
        BookMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/all")
    public Result<?> getAll() {
        return Result.success(BookMapper.selectList(null));
    }


    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(BookMapper.selectById(id));
    }


    @GetMapping("/getRecommendBook/{user_id}")
    public Result<?> getRecommendBook(@PathVariable Integer user_id) {
        List<Book> books = BookMapper.getRecommendBook(user_id);
        for (Book book : books) {
            System.out.println(book);
        }
        return Result.success(books);
    }

    @GetMapping("/getRecommend/{user_id}")
    public Result<?> getRecommend(@PathVariable Integer user_id) {
        List<Book> books = BookMapper.getRecommend(user_id);
        for (Book book : books) {
            System.out.println(book);
        }
        return Result.success(books);
    }


    @GetMapping("/classify/{type}")
    public Result<?> getType(@PathVariable String type) {
        return Result.success(BookMapper.selectList(Wrappers.<Book>lambdaQuery().like(Book::getType,type)));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(defaultValue = "") String search){
        return Result.success(BookMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<Book>lambdaQuery().like(Book::getName,search)));
    }
}
